package com.guzhi.controller;

import com.guzhi.anno.Log;
import com.guzhi.pojo.Emp;
import com.guzhi.pojo.EmpQueryParam;
import com.guzhi.pojo.PageResult;
import com.guzhi.pojo.Result;
import com.guzhi.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/emps")
@RestController
public class EmpController {
    private static final Logger log = LoggerFactory.getLogger(EmpController.class);

    @Autowired
    private EmpService empService;

    // 分页查询
    @GetMapping
    public Result page(EmpQueryParam  param) {
        log.info("分页查询:{}",param);
        PageResult<Emp> pageResult=empService.page(param);
        return Result.success(pageResult);
    }

    // 新增员工
    @PostMapping
    public Result save(@RequestBody Emp  emp){
        log.info("新增员工:{}",emp);
        empService.save(emp);
        return Result.success();
    }

    //删除员工
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids){
        log.info("删除员工:{}",ids);
        empService.delete(ids);
        return Result.success();
    }

    //根据ID查询员工
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("根据ID查询员工:{}",id);
        Emp emp = empService.findById(id);
        return Result.success(emp);
    }
    //修改员工
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工:{}",emp);
        empService.update(emp);
        return Result.success();
    }
}
